package com.glsc.ngateway.common.base.repo.mysql.gateway.oaflow;

import com.glsc.ngateway.common.base.domain.mysql.gateway.oaflow.xirtrade.TOaFlowXirTradeInfo;
import com.glsc.ngateway.common.api.oaflow.dto.xirtrade.TOaFlowXirTradeInfoSearchParamDto;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.validation.constraints.NotNull;
import java.util.List;


@Repository
public interface TOaFlowXirTradeInfoRepository extends JpaRepository<TOaFlowXirTradeInfo, Long> {

    TOaFlowXirTradeInfo findByRequestId(String requestId);

    List<TOaFlowXirTradeInfo> findAllByIntordidInAndXirBizTypeIn(List<String> allIntordIds, List<String> asList);

    @Query(value = "select t from TOaFlowXirTradeInfo t " +
            "where t.deleted=0" +
            "and (coalesce(:#{#instIds}, null) is null OR t.instId in (:#{#instIds} ))  " +
            "and (:#{#type} is null OR t.xirBizType = :#{#type} )  " +
            "and (coalesce(:#{#intordIds}, null) is null OR t.intordid in (:#{#intordIds} ))  " +
            "")
    List<TOaFlowXirTradeInfo> findAllByInstIdInAndXirBizTypeAndIntordidIn(List<String> instIds, String type, List<String> intordIds);

    @Query(value = "select t from TOaFlowXirTradeInfo t " +
            "where t.deleted=0" +
            "and (t.xirBizType = :type )  " +
            "and (t.intordid in :intordIds )  " +
            "")
    List<TOaFlowXirTradeInfo> findAllByXirBizTypeAndIntordidIn(String type, List<String> intordIds);

    @Query(value = "select * from t_oa_flow_xir_trade_info t " +
            "where t.deleted=0 " +
            "and if( :#{#param.dealStatus} is not null, t.deal_status = :#{#param.dealStatus}, 1=1 ) " +
            "and if( :#{#param.requestTitle} is not null, t.request_title like CONCAT('%', :#{#param.requestTitle}, '%'), 1=1 ) " +
            "and if( :#{#param.intordid} is not null, t.intordid = :#{#param.intordid}, 1=1 ) " +
            "and if( :#{#param.sysordid} is not null, t.sysordid = :#{#param.sysordid}, 1=1 ) " +
            "and if( :#{#param.xirBizType} is not null, t.xir_biz_type = :#{#param.xirBizType}, 1=1 ) " +
            "and if( :#{#param.dealer} is not null, t.dealer like CONCAT('%', :#{#param.dealer}, '%'), 1=1 ) " +
            "and if( :#{#param.settleDateMin} is not null,  t.sync_time >= :#{#param.settleDateMin}, 1=1 ) " +
            "and if( :#{#param.settleDateMax} is not null,  t.sync_time <= :#{#param.settleDateMax}, 1=1 ) " +
            "and if( :#{#param.applicant} is not null,  t.applicant <= :#{#param.applicant}, 1=1 ) " +
            "and (coalesce(:#{#param.deptTitleList}, null) is null or t.dept_title in (:#{#param.deptTitleList})) " +
            "", nativeQuery = true)
    Page<TOaFlowXirTradeInfo> page(Pageable pageable, @NotNull @Param("param") TOaFlowXirTradeInfoSearchParamDto param);
}
